package com.yc.cloudvideo.sp_store.mapper;

import com.yc.cloudvideo.entity.*;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author LYYT
 * @since 2022-08-03
 */
public interface StoreMapper extends BaseMapper<Store> {
    //查询所有的仓库文件
    @Select("select * from store_file where cid=#{cid} order by id desc")
    @Results(value = {
            @Result(property = "fid",column = "fid",id = true),
            @Result(property = "file",column = "fid",
                    one = @One(select = "com.yc.cloudvideo.sp_store.mapper.FileMapper.selectFileByFid"))
    })
    List<StoreFile> selectFilesByCid(int cid);

    //查询仓库名字
    @Select("select cname from store where cid=#{cid}")
    String selectNameByCid(int cid);

    //获取仓库路径字段
    @Select("select path from store where cid=#{cid}")
    String selectPathByCid(int cid);

    //查询仓库信息，关联查询仓库所属者信息
    @Select("select * from store where cid=#{cid}")
    @Results(value = {
            @Result(property = "uid",column = "uid",id = true),
            @Result(property = "user",column = "uid",
                    one = @One(select = "com.yc.cloudvideo.sp_store.mapper.UserMapper.selectUserByUid"))
    })
    Store selectByCid(int cid);

    //查询仓库信息
    @Select("select * from store_user where uid=#{uid}")
    @Results({
            //MyBatis 默认字段映射只能映射一次
            @Result(property = "cid",column ="cid"),
            @Result(property = "store",column ="cid",
                    one = @One(select="selectStoreByCid"))
    })
    List<StoreUser> selectByUid(int uid);

    //查询仓库
    @Select("select * from store where cid=#{cid}")
    @Results({
            //MyBatis 默认字段映射只能映射一次
            @Result(property = "cid",column ="cid"),
            @Result(property = "user",column ="cid",
                    one = @One(select="selectByCid2"))
    })
    Store selectStoreByCid(int cid);

    //查询公开仓库
    @Select("select * from store_user where uid=#{uid}")
    @Results({
            //MyBatis 默认字段映射只能映射一次
            @Result(property = "cid",column ="cid"),
            @Result(property = "store",column ="cid",
                    one = @One(select="selectPublicByOvert"))
    })
    List<StoreUser> selectPublicStoreByOvert(int uid);
    //查询公开仓库
    @Select("select * from store where cid=#{cid} and overt=1")
    @Results({
            //MyBatis 默认字段映射只能映射一次
            @Result(property = "cid",column ="cid"),
            @Result(property = "user",column ="cid",
                    one = @One(select="selectByCid2"))
    })
    Store selectPublicByOvert(int cid);

    //查询私有仓库
    @Select("select * from store_user where uid=#{uid}")
    @Results({
            //MyBatis 默认字段映射只能映射一次
            @Result(property = "cid",column ="cid"),
            @Result(property = "store",column ="cid",
                    one = @One(select="selectPrivateByOvert"))
    })
    List<StoreUser> selectPrivateStoreByOvert(int uid);
    //查询私有仓库
    @Select("select * from store where cid=#{cid} and overt=0")
    @Results({
            //MyBatis 默认字段映射只能映射一次
            @Result(property = "cid",column ="cid"),
            @Result(property = "user",column ="cid",
                    one = @One(select="selectByCid2"))
    })
    Store selectPrivateByOvert(int cid);



    //查询创建者信息
    @Select("select u.* from store_user s join user u on s.uid=u.uid where cid=#{cid}  and level=1")
    User selectByCid2(int cid);

}
